package com.naiking.util
{
	/**
	 * @author Nai.King
	 * 创建时间：2013-8-26 下午4:46:43
	 * UI工具类
	 */
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.InteractiveObject;
	import flash.display.Sprite;
	import flash.filters.ColorMatrixFilter;
	import flash.geom.Matrix;
	import flash.geom.Point;
	import flash.text.TextField;
	import flash.text.TextFormat;
	
	public class UIUtil
	{
		/**获取displayobject的全局坐标*/
		public static function getGlobalPoint(dspObj:DisplayObject):Point
		{
			return dspObj.localToGlobal(new Point(0,0));
		}
		/**设置文本的默认格式*/
		public static function setTextFormat(tf:TextField,font:String=null,size:int=12,
											color:uint=0x000000,bold:Boolean=false):void
		{
			var tfmt:TextFormat=new TextFormat(font,size,color,bold);
			tf.defaultTextFormat=tfmt;
		}
		public static function addDisplayObject(_dspObj:DisplayObject,
												_parent:DisplayObjectContainer,
												_x:int=0,
												_y:int=0):void
		{
			_parent.addChild(_dspObj);
			_dspObj.x=_x;
			_dspObj.y=_y;
		}
		public static function drawRectTo(sp:Sprite,color:uint,alpha:Number=1,x:int=0,y:int=0,w:int=100,h:int=100):void
		{
			if(sp)
			{
				sp.graphics.beginFill(color,alpha);
				sp.graphics.drawRect(x,y,w,h);
				sp.graphics.endFill();
			}
		}
		public static function removeChild(dspObj:DisplayObject):void
		{
			if(dspObj.parent)
				dspObj.parent.removeChild(dspObj);
		}
		public static function removeAllChild(dspCon:DisplayObjectContainer):void
		{
			var len:int=dspCon.numChildren;
			for(var i:int=0;i<len;i++)
			{
				dspCon.removeChildAt(0);
			}
		}
		public static function setXY(dis:DisplayObject,_x:int,_y:int):void
		{
			dis.x=_x;
			dis.y=_y;
		}
		public function UIUtil()
		{
		}
		/**设置禁用 可用状态 含mousenable*/
		public static function setEnabled(obj:InteractiveObject,enabled:Boolean):void
		{
			obj.mouseEnabled = enabled;
			if(enabled)
			{
				obj.filters=[];
			}else
			{
				var matrix:Array = new Array(); 
				matrix = matrix.concat([.3,.59,.11,0,0]);// red 
				matrix = matrix.concat([.3,.59,.11,0,0]);// green 
				matrix = matrix.concat([.3,.59,.11,0,0]);// blue 
				matrix = matrix.concat([0,0,0,1,0]);// alpha  
				var filter:ColorMatrixFilter = new ColorMatrixFilter(matrix); 
				obj.filters = [filter];
			}
		}
		/**翻转*/
		public static function flipHorizontal(dsp:DisplayObject):void
		{
			var matrix:Matrix = dsp.transform.matrix;
			matrix.a=-1;
			matrix.tx=dsp.width+dsp.x;
			dsp.transform.matrix=matrix;
		}
		/**翻转*/
		public static function flipVertical(dsp:DisplayObject):void
		{
			var matrix:Matrix = dsp.transform.matrix;
			matrix.d=-1;
			matrix.ty=dsp.height+dsp.y;
			dsp.transform.matrix=matrix;
		} 
	}
}
